package com.xiaoxin.executor.batch.writer;

import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class CustomItemWriter<T> implements ItemWriter<T> {
    private Map<String, ItemWriter<T>> writerMap;


    private List<T> list1 = new ArrayList<>();
    private List<T> list2 = new ArrayList<>();

    @BeforeStep
    public void beforeStep(StepExecution stepExecution) {
        // 清空list，避免上一次运行的结果影响本次运行
        list1.clear();
        list2.clear();
    }

    @Override
    public void write(List<? extends T> items) throws Exception {
        writeToDataSource(list1, "even");
    }

    private void writeToDataSource(List<? extends T> list, String type) throws Exception {
        System.out.println("Writing " + type + " numbers: " + list);
        writerMap.get(type).write(list);
        // 这里可以添加将数据写入数据库或其他地方的逻辑
    }


}
